/*
** Upload to flickr
**
** Author........ : Kevin Xiong
** Website....... : http://blog.btkx.net
**
*/

var optionsMap = {};

function preUpload(img) {
    createDiv(img.src, img.x, img.y);
}

function createDiv(url, x, y) {
    var div_id = "div_" + $.md5(url);
    if ($("#"+div_id)[0]) {
    	$("#"+div_id).show();
    } else {
        var parentDiv=$('<div></div>');
        parentDiv.attr('id',div_id);
        parentDiv.css("position","absolute");
        parentDiv.css("display","block");
        parentDiv.css("left", x);
        parentDiv.css("top", y);
        parentDiv.css("z-index", "99999");

        parentDiv.append("<a target='_blank' alt='Upload to Flickr!' href='http://www.flickr.com/tools/sendto.gne?url="+url+"'><img alt='Upload to Flickr!' src='"+ chrome.extension.getURL('upload.') + optionsMap["hint_icon"] +".png'/></a>");
        parentDiv.appendTo('body');
    }
    var hint_fadeout_time = optionsMap["hint_fadeout_time"];
    $("#"+div_id).fadeOut(hint_fadeout_time);
}

function autoRun() {
    chrome.extension.sendRequest({cmd:"getOptions" }, function(response) {
        optionsMap = JSON.parse(response); 
        var min_img_width = optionsMap["min_img_width"];
        var min_img_height = optionsMap["min_img_height"];
        $("img").each(function(){
            if ($(this).width()>=min_img_width & $(this).height()>=min_img_height ) {
                $(this).mouseenter(function(){preUpload(this);});
            }
        });
    });

}

$(window).load(autoRun());
